package com.nit.lab.mapper.basic;

import com.nit.lab.dto.basic.GoodsDTO;
import com.nit.lab.dto.basic.GoodsListDTO;
import org.apache.ibatis.annotations.*;
import com.nit.lab.entity.basic.GoodsEntity;
import com.nit.lab.entity.basic.GoodsListEntity;
import org.apache.ibatis.annotations.Param;

import java.util.Date;
import java.util.List;

public interface GoodsListMapper {

    /**
     * @Description: 获取表格数据，查询条件自行编写对应代码，需区分模糊和相等，排序条件无需改动
     * @Author: zb
     * @Date: 2022/06/28 14:23
     */
    @Select({"<script>SELECT " +
            "list_id AS `key`,list_id, " +
            "goods_num, goods_name, goods_desc, creator, photo_url,goods_unit, " +
            "create_time, show_time, goods_status " +
            "FROM goods_list " +
            "WHERE TRUE " +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "ORDER BY " +
            "list_id desc " +
            "LIMIT #{start}, #{length}" +
            "</script>"})

    List<GoodsListDTO> list4Table(
            @Param("start") Integer start,
            @Param("length") Integer length,
            @Param("goodsName") String goodsName,
            @Param("sortField") String sortField,
            @Param("sortOrder") String sortOrder
    ) throws Exception;

    /**
     * @Description: 获取表格数据记录的总条数
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Select({"<script>SELECT COUNT(*) " +
            "FROM goods_list " +
            "WHERE TRUE " +
            "<if test='goodsName != null'> AND goods_name LIKE CONCAT('%', #{goodsName}, '%') </if>" +
            "</script>"})
    Long count4Table(
            @Param("goodsName") String goodsName
    ) throws Exception;


    /**
     * @description: 更新
     * @Author: WJM
     * @Date: 2022/7/5 0:03
     **/
    @Update("UPDATE goods_list set " +
            "goods_name = #{GoodsListEntity.goodsName}, " +
            "goods_desc = #{GoodsListEntity.goodsDesc}, " +
            "photo_url = #{GoodsListEntity.photoUrl}, " +
            "creator = #{GoodsListEntity.creator}, " +
            "show_time = #{GoodsListEntity.showTime}, " +
            "goods_num = #{GoodsListEntity.goodsNum} " +
            "WHERE list_id = #{GoodsListEntity.listId}")
    Integer update(@Param("GoodsListEntity") GoodsListEntity goodsListEntity) throws Exception;
    /**
     * @Description: 删除记录
     * @Author: SN
     * @Date: 2019/01/30 18:15
     */
    @Delete("DELETE FROM goods_list WHERE list_id = #{listId}")
    Integer remove1(@Param("listId") Long listId) throws Exception;
    /**
     * @Description: 获取表格数据,接口需返回json格式
     * @Author: skw
     * @Date: 2022/06/28 14:23
     */
    @Select({"<script>SELECT " +
            "list_id, goods_num, "+
            "goods_name, goods_desc, creator, "+
            "photo_url, show_time, create_time, goods_unit, goods_status  "+
            "FROM goods_list "+
            "ORDER BY list_id desc " +
            "</script>"})
    List<GoodsListDTO> listGoodsData() throws Exception;

    /**
     * @Description: 获取对应展示id的商品数量
     * @Author: skw
     * @Date: 2022/06/28 14:23
     */
    @Select({"<script>SELECT goods_num " +
            "FROM goods_list " +
            "WHERE list_id = #{listId}" +
            "</script>"})
    Long getNum(
            @Param("listId") Long listId
    ) throws Exception;


    /**
     * @Description: 根据商品ID获取商品信息
     * @Author: zb
     * @Date: 2022/06/28 14:23
     */
    @Select({"<script>SELECT " +
            "list_id, goods_name, goods_desc, " +
            "creator, photo_url, goods_num, show_time, create_time, show_time, goods_unit, goods_status " +
            "FROM goods_list " +
            "WHERE list_id= #{listId} " +
            "</script>"})
    List<GoodsListDTO> getGoodsInfo(
            @Param("listId") Integer listId
    ) throws Exception;
    /**
     * @Description: 根据goodsID获取具体某条商品信息
     * @Author: zb
     * @Date: 2022/06/28 14:23
     */
    @Select({"<script>SELECT " +
            "goods_id, token_id, creator, t2.user_name owner2, " +
            "owner, photo_url, goods_name, goods_desc, status, create_time, modify_time, list_id, goods_number, hash_id " +
            "FROM basic_goods t1 " +
            "LEFT JOIN basic_user t2 ON t1.owner = t2.user_info " +
            "WHERE hash_id= #{hashId} " +
            "</script>"})
    List<GoodsDTO> getBasicGoodsInfo(
            @Param("hashId") String hashId
    ) throws Exception;
    /**
     * @Description: 删除从表对应记录
     * @Author: zb
     * @Date: 2022/06/28 14:23
     */
    @Delete("DELETE FROM basic_goods WHERE list_id = #{listId}")
    Integer remove2(@Param("listId") Long listId) throws Exception;

    /**
     * @description: 添加主表的商品
     * @Author: WJM
     * @Date: 2022/7/12
     **/
    @Insert("INSERT INTO goods_list (" +
            "goods_name, goods_num, goods_desc, creator, photo_url, show_time, goods_unit, goods_status )" +
            "VALUES (#{GoodsListEntity.goodsName}, #{GoodsListEntity.goodsNum}, " +
            "#{GoodsListEntity.goodsDesc}, #{GoodsListEntity.creator}, #{GoodsListEntity.photoUrl}, #{GoodsListEntity.showTime}, #{GoodsListEntity.goodsUnit}, 0 ) ")
    Integer add(@Param("GoodsListEntity") GoodsListEntity goodsListEntity) throws Exception;
    /**
     * @description: 将goods_status栏由0改为1
     * @Author: SKW
     * @Date: 2022/7/12
     **/
    @Update("UPDATE goods_list " +
            "SET goods_status = 1 " +
            "WHERE list_id = #{listId} ")
    Integer editStatus(@Param("listId") Integer listId) throws Exception;

    @Select("SELECT goods_num FROM goods_list " +
            "WHERE list_id = #{id}")
    Integer getGoodsNum(@Param("id") Integer listId) throws Exception;

    @Select("SELECT show_time FROM goods_list " +
            "WHERE list_id = #{id}")
    Date getShowTime(@Param("id") Integer listId) throws Exception;

    /**
     * @description:
     * @Author: WJM
     * @Date: 2022/7/12 16:20
     **/
    @Delete("DELETE FROM basic_goods WHERE list_id = #{goodsId}")
    Integer remove(@Param("goodsId") Long goodsId) throws Exception;

    /**
     * @description:
     * @Author: WJM
     * @Date: 2022/7/12
     **/
    @Update("UPDATE goods_list " +
            "SET goods_num = goods_num+#{num} " +
            "WHERE list_id = #{listId} ")
    Integer addNum(@Param("listId") Integer listId, @Param("num") Integer num) throws Exception;
    // 根据goods表里的goodsId减库存
    @Update("UPDATE goods_list " +
            "SET goods_num = goods_num-1 " +
            "WHERE list_id = #{listId} ")
    Integer dropNum(@Param("listId") Integer listId) throws Exception;

    /**
     * @description: 根据listIds去获取所包含的所有商品的goodsId
     * @Author: WJM
     * @Date: 2022/7/17 15:42
     **/
    @Select("SELECT * " +
            "FROM basic_goods " +
            "WHERE list_id = #{listId} ")
    List<GoodsDTO> getGoodsId(@Param("listId") Integer listId) throws Exception;
}
